function b = subsref(s,index)
% SUBSREF Define field name indexing for  objects
switch index.type
    case '()'
        error('Unsupported');
    case '.'
        switch index.subs
			case 'mode_id'
				b = s.mode_id;
			case 'mode_value'
				b = s.mode_value;
            case 'sampling_freq'
                b = s.sampling_freq;
            case 'r2'
                b = calc_r2(s);
            case 'm2'
                b = calc_m2(s);
            otherwise
                error('Undefined field name');
        end
end
end

function r2 = calc_r2(s)
mode_id = s.mode_id;
mode_value = s.mode_value;
mode_num = length(mode_id);
sampling_freq = s.sampling_freq;
l = (mode_num-1)*2;
dx=  1/sampling_freq;
L = l*dx;

r2 = 1/L*sum(mode_value(1,2:end).^2+mode_value(2,2:end).^2);
end

function m2 = calc_m2(s)
mode_id = s.mode_id;
mode_value = s.mode_value;
mode_num = length(mode_id);
sampling_freq = s.sampling_freq;
l = (mode_num-1)*2;
dx=  1/sampling_freq;
L = l*dx;

K = zeros(size(mode_id));
K(2:end) = 4*sin(pi*mode_id(2:end)/l).^2/(L*dx^2);
m2 = sum(K(2:end).*mode_value(1,2:end).^2+K(2:end).*mode_value(2,2:end).^2);
end
